var app = angular.module('App', []);
app.controller('loginCtrl', ['$scope', '$timeout', '$http', function($scope, $timeout, $http) {
    var redirect = window.location.search.replace(/^\?/, '');
    // 初始化为空 
    var getCorporationList = function() {
        $http({
            method: 'get',
            url: '/register/corporation'
        }).success(function(data) {
            $scope.corporationList = data;
        });
    };
    getCorporationList();
    $scope.showLoginFail = false;
    $scope.showRetrievePassword = false;
    $scope.showRegisterSuccess = false;


    $scope.login = function(event) {
        event.stopPropagation();
        event.preventDefault();
        if (!$scope.userName || !$scope.password) {
            return;
        } else {
            $http({
                method: 'post',
                url: '/login/matchCode',
                data: {
                    userName: $scope.userName,
                    password: $scope.password,
                    redirect: redirect
                }
            }).success(function(data) {
                if (data.toString() === 'true') {
                    var path = '/' + decodeURI(redirect);
                    window.location = path;
                } else {
                    $scope.showLoginFail = true;
                }
            });
        }
    };


    /** 用户名更改 */
    $scope.userNameChange = function(ele) {
        delayedFn(queryIsUserNameOccupied);
    };

    /** 密码是否一致 */
    $scope.passwordChange = function() {
        checkPasswordEqual();
    };

    //名字是否被占用
    $scope.userNameOccupied = null;
    //密码是否一致
    $scope.passwordEqual = null;
    var timerPromise = null;

    /** 判断用户名是否被占用 */
    function queryIsUserNameOccupied() {
        if (!$scope.userName) return;
        //向服务器询问,用户名是否被占用?
        $http({
            method: 'get',
            url: '/register/usernameoccupied/' + $scope.userName
        }).success(function(data) {
            if (data.toString() === 'true') {
                $scope.userNameOccupied = true;
            } else {
                $scope.userNameOccupied = false;
            }
        });
    }
    var shake = (function() {
        var shakeRun = false;
        function shake() {
            if (shakeRun) return;
            var loginBtn = document.getElementById('form-wrapper');
            shakeRun = true;
            angular.element(loginBtn).addClass('shake');
            $timeout(function() {
                shakeRun = false;
                angular.element(loginBtn).removeClass('shake');
            }, 800);
        };
        return shake;
    })();

    /** 判断密码是否一致 */
    function checkPasswordEqual() {
        if ($scope.password1 === $scope.password2) {
            $scope.passwordEqual = true;
        } else {
            $scope.passwordEqual = false;
        }
    }

    /** 延迟加载函数 */
    function delayedFn(fn) {
        timerPromise && $timeout.cancel(timerPromise);
        timerPromise = $timeout(fn, 800);
    }
    
    $scope.submit = function(event) {
        if ($scope.corporation && $scope.department && $scope.reviewer && $scope.realName && $scope.phone && $scope.mail) {
            window.alert('请求已发送,请等待审核');
        } else {
            event.preventDefault();
            shake();
        }
    };

}]);